package cn.nawang.ebeim.uc;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;

import cn.nawang.ebeim.uc.Dngj;
import cn.nawang.ebeim.uc.EbeimEngine;
import cn.nawang.ebeim.uc.RestServer;
import cn.nawang.ebeim.uc.util.ConfigUtil;
import cn.nawang.ebeim.uc.util.Path;

/**
 * 等待进度条程序
 *
 */
public class ProgressBar implements ActionListener {

    private static final String DEFAULT_STATUS = "Please Waiting";
    private JDialog dialog;
    private JProgressBar progressBar;
    private JLabel lbStatus;
    private JButton btnCancel;
    private Window parent;
    private Thread client; // 处理客户端线程
    private RestServer ui;//处理UI端线程
    private String statusInfo;
    private String resultInfo;
    private String cancelInfo;
    /**
     * 显示进度条测试对话框
     * @param parent
     * @param thread
     */
    public static void show(Window parent, Thread thread) {
        new ProgressBar(parent, thread,null, DEFAULT_STATUS, null, null);
    }
    /**
     * 显示进度条测试对话框
     * @param parent
     * @param thread
     * @param statusInfo
     */
    public static void show(Window parent, Thread thread, String statusInfo) {
        new ProgressBar(parent, thread,null, statusInfo, null, null);
    }

    /**
     * 显示对话框
     * @param parent
     * @param thread
     * @param statusInfo
     * @param resultInfo
     * @param cancelInfo
     */
    public static void show(Window parent, Thread thread,RestServer ui,String statusInfo,
            String resultInfo, String cancelInfo) {
        new ProgressBar(parent, thread,ui, statusInfo, resultInfo, cancelInfo);
    }

    
    public ProgressBar(){
      
    }
    /**
     * 对话框构造函数
     * @param parent
     * @param thread
     * @param statusInfo
     * @param resultInfo
     * @param cancelInfo
     */
    private ProgressBar(Window parent, Thread thread,RestServer ui, String statusInfo,
            String resultInfo, String cancelInfo) {
        this.parent = parent;
        this.client = thread;
        this.ui = ui;
        this.statusInfo = statusInfo;
        this.resultInfo = resultInfo;
        this.cancelInfo = cancelInfo;
        initUI();//初始化进度条
        startThread();//处理耗时线程
        dialog.setVisible(true);
    }
    /**
     * 构建显示进度条的对话框
     */
    private void initUI() {
        if (parent instanceof Dialog) {
            dialog = new JDialog((Dialog) parent, true);
        } else if (parent instanceof Frame) {
            dialog = new JDialog((Frame) parent, true);
        } else {
            dialog = new JDialog((Frame) null, true);
        }

        final JPanel mainPane = new JPanel(null);
        progressBar = new JProgressBar();
        lbStatus = new JLabel("<html><font color=rgb(51,51,51) face='微软雅黑'>正在为您初始化易备安客户端软件，请稍后...</font></html>");
        final ImageIcon icon = new ImageIcon(Path.path()+"image/wait.jpg");  
        JPanel logo = new JPanel() {  
          protected void paintComponent(Graphics g) {  
              java.awt.Image img = icon.getImage();  
              g.drawImage(img, 0, 0,390,  
                      255, icon.getImageObserver());
          }  
        };
        logo.setBounds(0, 0, 390, 200);
        mainPane.add(logo,BorderLayout.NORTH);
        progressBar.setIndeterminate(true);
        java.awt.Color barRgb = new java.awt.Color(26, 84, 142);
        java.awt.Color rgb = new java.awt.Color(255, 255, 255);
        progressBar.setForeground(barRgb);
        progressBar.setBackground(rgb);
        mainPane.add(progressBar, BorderLayout.SOUTH);
        mainPane.add(lbStatus,BorderLayout.SOUTH);
        mainPane.setBackground(rgb);
        mainPane.setBounds(0, 0, 390, 39);
        dialog.getContentPane().add(mainPane);
        dialog.setUndecorated(true); // 除去title
        dialog.setResizable(true);
        dialog.setSize(390, 265);
        dialog.setLocationRelativeTo(parent); // 设置此窗口相对于指定组件的位置
        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); // 不允许关闭
        mainPane.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                layout(mainPane.getWidth(), mainPane.getHeight());
            }
        });
    }
    /**
     * 启动线程，测试进度条
     */
    private void startThread() {
        new Thread() {
            public void run() {
                try {
                    client.start(); // 处理耗时任务
                    ui.start();
                    // 等待事务处理线程结束
                    Thread.sleep(5000);
                    dialog.dispose();
                    new Dngj();//程序启动后显示主界面
                    ui.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                } finally {
                   
                }
            }
        }.start();
    }
    /**
     * 设置控件的位置和大小
     * @param width
     * @param height
     */
    private void layout(int width, int height) {
        progressBar.setBounds(20, 225, 350, 8);
        lbStatus.setBounds(20, 235, 350, 25);
    }

    @SuppressWarnings("deprecation")
    public void actionPerformed(ActionEvent e) {
        resultInfo = cancelInfo;
        client.stop();
    }

    public void start(){
      Thread engine = new Thread(new EbeimEngine());
      RestServer ui = new RestServer(ConfigUtil.getRestServerPort());
        //显示进度条测试对话框
      ProgressBar.show((Frame) null, engine,ui, "正在为您初始化易备安客户端软件，请稍后.....", "Result", "Cancel");
    }
    
    public static void main(String[] args) {
      ProgressBar pb = new ProgressBar();
      pb.start();
    }
}